<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增村民购煤订单')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-clientUserorder-add">
            <input type="hidden" id="villageId" name="villageId" th:value="*{villageId}">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">查找居民：</label>
                <div class="col-sm-8">
                    <div class="form-group">
                        <select class="form-control" id="user_select">
                            <option value=""></option>
                        </select>
                    </div>
                    <input name="clientUserId" id="clientUserId" class="form-control" type="hidden" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">姓名：</label>
                <div class="col-sm-8">
                    <label id="name_div"></label>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">身份证号：</label>
                <div class="col-sm-8">
                    <label id="card_div"></label>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">购煤数量（吨）：</label>
                <div class="col-sm-8">
                    <input name="amount" id="amount" class="form-control number" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">购煤款（元）：</label>
                <div class="col-sm-8">
                    <input name="money" id="money" class="form-control number" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">收到煤时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="takeinTime" type="text" class="form-control" placeholder="年-月-日">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </form>
        <div class="box-footer">
            <div class="col-sm-offset-5 col-sm-10">
                <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
<!--                <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>-->
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "jjm/clientUserorder";
        var type = [[${type}]];
        $("#form-clientUserorder-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-clientUserorder-add').serialize());
            }
        }

        $("input[name='takeinTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        $(function () {
            getUser();
            $("#user_select").on("change",function () {
                var clientUserId = $(this).select2("val");
                var text = $(this).select2('data')[0].text;
                var userInfo = text.split("(");
                var idInfo = clientUserId.split(',');
                $("#clientUserId").val(idInfo[0]);
                $("#amount").val(idInfo[1]);
                $("#money").val(idInfo[2]);
                $("#name_div").text(userInfo[0]);
                $("#card_div").text(userInfo[1].split(")")[0]);
            })
        });

        function getUser() {
            var url = prefix+"/listDeliverClientUser";
            var villageId = $("#villageId").val();
            if(villageId){
                url = url + "?villageId="+villageId;
            }
            $.get(url,
                function (data) {
                    console.log(data);
                    $("#user_select").empty();
                    $("#user_select").append('<option>请选择</option>')
                    if (data.code == 0) {
                        commonSelect(data.data,"#user_select","");
                    }
                });
        }
        function commonSelect(res,selector,placeholder) {
            var Arr = [];
            var sel = $(selector);
            sel.html("<option></option>");
            $.each(res, function (i, item) {
                Arr.push({id: item.id+","+item.amount+","+item.money, text: item.masterName+"("+item.masterIdentityCardNumber+")"})
            });
            sel.select2({data: Arr, placeholder: placeholder, disabled: false});

        }

        $("input[name='takeinTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>